home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / events.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  2.5 KB  |  91 lines

  1.  
  2. __doc__ = """
  3. Dirt Simple Events
  4.  
  5. A Dispatcher (or a subclass of Dispatcher) stores event handlers that
  6. are 'fired' simple event objects when interesting things happen.
  7.  
  8. Create a dispatcher:
  9.  
  10.   >>> d = Dispatcher()
  11.  
  12. Now create a handler for the event and subscribe it to the dispatcher
  13. to handle Event events.  A handler is a simple function or method that
  14. accepts the event as an argument:
  15.  
  16.   >>> def handler1(event): print `event`
  17.   >>> d.subscribe(Event, handler1)
  18.  
  19. Now dispatch a new event into the dispatcher, and see handler1 get
  20. fired:
  21.  
  22.   >>> d.dispatch(Event(foo='bar', data='yours', used_by='the event handlers'))
  23.   <rdflib.events.Event ['data', 'foo', 'used_by']>
  24. """
  25.  
  26. class Event(object):
  27.     """
  28.     An event is a container for attributes.  The source of an event
  29.     creates this object, or a subclass, gives it any kind of data that
  30.     the events handlers need to handle the event, and then calls
  31.     notify(event).
  32.  
  33.     The target of an event registers a function to handle the event it
  34.     is interested with subscribe().  When a sources calls
  35.     notify(event), each subscriber to that even will be called i no
  36.     particular order.
  37.     """
  38.  
  39.     def __init__(self, **kw):
  40.         self.__dict__.update(kw)
  41.  
  42.     def __repr__(self):
  43.         attrs = self.__dict__.keys()
  44.         attrs.sort()
  45.         return '<rdflib.events.Event %s>' % ([a for a in attrs],)
  46.  
  47.  
  48. class Dispatcher(object):
  49.     """
  50.     An object that can dispatch events to a privately managed group of
  51.     subscribers.
  52.     """
  53.  
  54.     _dispatch_map = None
  55.  
  56.     def set_map(self, amap):
  57.         self._dispatch_map = amap
  58.  
  59.     def get_map(self):
  60.         return self._dispatch_map
  61.  
  62.     def subscribe(self, event_type, handler):
  63.         """ Subscribe the given handler to an event_type.  Handlers
  64.         are called in the order they are subscribed.
  65.         """
  66.         if self._dispatch_map is None:
  67.             self.set_map({})
  68.         lst = self._dispatch_map.get(event_type, None)
  69.         if lst is None:
  70.             lst = [handler]
  71.         else:
  72.             lst.append(handler)
  73.         self._dispatch_map[event_type] = lst
  74.  
  75.     def dispatch(self, event):
  76.         """ Dispatch the given event to the subscribed handlers for
  77.         the event's type"""
  78.         if self._dispatch_map is not None:
  79.             lst = self._dispatch_map.get(type(event), None)
  80.             if lst is None:
  81.                 raise ValueError("unknown event type: %s" % type(event))
  82.             for l in lst:
  83.                 l(event)
  84.  
  85. def test():
  86.     import doctest
  87.     doctest.testmod()
  88.  
  89. if __name__ == '__main__':
  90.     test()
  91.